#include "teacharmsitem.h"
#include "toolkit.h"
#include <QPainter>
#include "resources.h"
#include <QDebug>
#include "game/teachgamescene.h"
#include "item/bullettrackitem.h"

const float beginy = 250;
const float bulletX = 44;

TeachArmsItem::TeachArmsItem():
    ArmsItem(CUP_MTP),
    m_iFireNum(0),
    m_bShowIndicate(false),
    m_scale(1.0)
{
    m_scaletimer.setInterval(200);
    connect(&m_scaletimer,SIGNAL(timeout()),this,SLOT(UpdateScale()));
    m_scaletimer.start();
}

void TeachArmsItem::SetDetectX()
{
    m_detectX = 310;
}

void TeachArmsItem::Fire()
{
    if(m_mananimatp == NULL_TP && m_iFireNum == 0)
    {
        if(ToolKit::m_bVoice && ToolKit::m_bgameVoice)
            ToolKit::playSound(11);

        SetAnimaType(SOS_FIRE_TP);
        initBullet();
    }
    else if(m_iFireNum == 1)
    {
        emit ShowIndicate();
    }

    ++m_iFireNum;
}

void TeachArmsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->drawPixmap(QRectF(m_ManPt,m_ManSize),m_animaimage,QRectF(QPointF(m_x,0),m_ManSize));

    if(m_mananimatp == CATCH_TP)
    {
       switch(m_CoverType)
       {
       case COMMON_CTP:
       {
           painter->drawPixmap(QRectF(m_CommonPt,m_CoverCommonSize),m_CoverCommonImage,
                               QRectF(QPointF(m_coverX,0),m_CoverCommonSize));
       }
           break;
       case DIS_CTP:
       {
           painter->drawPixmap(QRectF(m_DisappearPt,m_CoverDisappearSize),m_CoverDisappearImage,
                               QRectF(QPointF(m_coverX,0),m_CoverDisappearSize));
       }
           break;
       case BOMB_CTP:
       {
           painter->drawPixmap(QRectF(m_DisappearPt,m_CoverBombSize),m_CoverBombImage,
                               QRectF(QPointF(m_coverX,0),m_CoverBombSize));
       }
           break;
       default:
           break;
       }
    }

    if(m_bShowIndicate)
    {
        painter->drawPixmap(QRectF(28.0,149,48 * m_scale,48 * m_scale),
                            ToolKit::m_rc->m_IndicateImage.transformed(QTransform().scale(m_scale,m_scale)),
                            QRectF(0,0,48 * m_scale,48 * m_scale));
    }

    if(m_bShowBullet)
    {
        painter->save();

        painter->setOpacity(m_fBulletOpacity);
        painter->setPen(QPen(QBrush(Qt::white),(m_manTp == CUP_MTP) ? 3 : 6));
        painter->drawLine(QPointF(bulletX,135),QPointF(bulletX,m_endY));

        painter->restore();
    }

    painter->drawRect(boundingRect());
}

void TeachArmsItem::UpdateScale()
{
    if(m_scale < 1.10)
    {
        m_scale += 0.1;
    }
    else if(m_scale > 1.0)
    {
        m_scale -= 0.1;
    }
}


void TeachArmsItem::Calculation()
{
    if(isVisible() && m_bCalculation && m_mananimatp == CATCH_TP )
    {
        if(m_linelist.isEmpty())
        {
            qWarning("cao jinle");
            m_PathList.clear();
            if(m_lineTp == Hatch_LTP && y() < 150.0)
            {
                if(ToolKit::m_bVoice && ToolKit::m_bgameVoice)
                    ToolKit::playSound(33);

                setVisible(false);
                m_bRun = false;

                m_flyItem = NULL;

                ToolKit::m_teachGameScene->CupIn();
                return;
            }
            else
            {
                m_lineTp = PT_LTP;
                m_Interval = 0.0;
                qWarning("wocaowocao");
                if(m_CoverType == COMMON_CTP && m_linelist.isEmpty() && m_PathList.isEmpty())
                {
                    QTimer::singleShot(3000,this,SLOT(CoverDisappear()));
                }
            }
        }
        else
        {
            if(m_Interval < 1.0)
            {
                m_Interval += m_FloatSpeed;

                if(m_Interval == m_FloatSpeed)
                {
                    while(m_PathList.count() - m_linelist.count() > 0)
                    {
                        m_PathList.removeLast();
                    }
                    m_linelist.removeLast();
                }

                Q_ASSERT(m_PathList.count() - m_linelist.count() == 1);

                for(int j = 0; j < m_linelist.count(); ++j)
                {
                    if(m_PathList.count() > 1)
                        m_linelist.replace(j,GetIntervalPt(m_PathList.at(j),m_PathList.at(j + 1),m_Interval));
                }

            }
            else
            {
                if(!m_PathList.isEmpty())
                {
                    if(m_son != NULL)
                    {
                        m_son->m_linelist.append(m_PathList.first());
                        m_son->m_PathList.append(m_PathList.first());
                    }

                    m_PathList.removeFirst();
                }

                Q_ASSERT(m_PathList.count() == m_linelist.count());

                for(int j = 0; j < m_linelist.count();++j)
                {
                    m_linelist.replace(j,m_PathList.at(j));
                }

                m_Interval = 0.0;
            }
        }
    }
}
